<template>
  <template v-for="item in data" :key="item.id">
    <el-sub-menu v-if="item.children && item.children.length" :index="item.path || String(item.id)">
      <template #title>
        <el-icon v-if="IconComp(item.icon)" class="menu-icon">
          <component :is="IconComp(item.icon)" />
        </el-icon>
        <span>{{ item.name }}</span>
      </template>
      <MenuTree :data="item.children" />
    </el-sub-menu>
    <el-menu-item v-else :index="item.path || String(item.id)">
      <el-icon v-if="IconComp(item.icon)" class="menu-icon">
        <component :is="IconComp(item.icon)" />
      </el-icon>
      <span>{{ item.name }}</span>
    </el-menu-item>
  </template>
</template>

<script setup>
import * as Icons from '@element-plus/icons-vue'
defineProps({ data: { type: Array, default: () => [] } })

function IconComp(name) {
  if (!name) return null
  return Icons[name] || null
}
</script>

<style scoped>
.menu-icon {
  margin-right: 8px;
  font-size: 16px;
}
</style>
